//
//  View.swift
//  picPost
//
//  Created by dream on 2024/1/5.
//

import Foundation
import UIKit

extension UIColor {
    func toImage() -> UIImage? {
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        
        // 创建上下文并设置大小与透明度
        UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        
        // 设置背景颜色
        context.setFillColor(cgColor)
        context.fill(rect)
        
        // 从当前上下文获取生成的图像
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        // 结束上下文
        UIGraphicsEndImageContext()
        
        return image
    }
}

extension UIView {
    
    @IBInspectable var insVGriLayer: Bool {
        set {
            if newValue {
                let gradientLayer = CAGradientLayer()
                gradientLayer.frame = bounds
                gradientLayer.colors = [
                    #colorLiteral(red: 0.2274509804, green: 0.3411764706, blue: 0.2901960784, alpha: 1).cgColor,
                    #colorLiteral(red: 0.1058823529, green: 0.1725490196, blue: 0.1411764706, alpha: 1).cgColor
                ]
                gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
                gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)

                layer.insertSublayer(gradientLayer, at: 0)
            }
        }
        get { return false }
    }
    
    @IBInspectable var farroRegularFont: CGFloat {
        set {
            let font = UIFont(name: "Farro-Regular", size: newValue)!
            setCustomFont(font)
        }
        get { return 0 }
    }
    
    @IBInspectable var farroLightFont: CGFloat {
        set {
            let font = UIFont(name: "Farro-Light", size: newValue)!
            setCustomFont(font)
        }
        get { return 0 }
    }
    
    @IBInspectable var farroMediumFont: CGFloat {
        set {
            let font = UIFont(name: "Farro-Medium", size: newValue)!
            setCustomFont(font)
        }
        get { return 0 }
    }
    
    @IBInspectable var farroBoldFont: CGFloat {
        set {
            let font = UIFont(name: "Farro-Bold", size: newValue)!
            setCustomFont(font)
        }
        get { return 0 }
    }
    
    private func setCustomFont(_ font : UIFont) {
        if self.isKind(of: UIButton.self) {
            let button = self as! UIButton
            button.titleLabel?.font = font
        }else if (self.isKind(of: UILabel.self)){
            let lab = self as! UILabel
            lab.font = font
        }else if (self.isKind(of: UITextField.self)){
            let field = self as! UITextField
            field.font = font
        }else if (self.isKind(of: UITextView.self)){
            let textView = self as! UITextView
            textView.font = font
        }
    }
}

typealias TapEvent = () -> Void
private var tapEventKey = "TapEventKey"

extension UIView {
    
    private var event : TapEvent {
        get {
            objc_getAssociatedObject(self, &tapEventKey) as! TapEvent
        }
        set {
            objc_setAssociatedObject(self, &tapEventKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
    }
    
    func addTapEvent(event : @escaping TapEvent) {
        self.event = event
        isUserInteractionEnabled = true
        addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tapEvent(ges:))))
    }
    
    @objc private func tapEvent(ges : UIGestureRecognizer) {
        self.event()
    }
    
}

extension UIButton {
    @IBInspectable var openDisOrNorType: Bool {
        set {
            if newValue {
                let disImage = #colorLiteral(red: 0.1137254902, green: 0.1294117647, blue: 0.1294117647, alpha: 1).toImage() ?? UIImage()
                let norImage = UIImage(named: "login_agree") ?? UIImage()
                
                setBackgroundImage(disImage, for: .disabled)
                setBackgroundImage(norImage, for: .normal)
                
                setTitleColor(.white.withAlphaComponent(0.5), for: .disabled)
                setTitleColor(.black, for: .normal)
            }
        }
        get { return false }
    }
}
